package users;

import java.io.Serializable;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import java.util.TreeSet;

import networkedmodules.Module;

public class UserGroup extends Module implements Serializable, Observer
{
    private static final long serialVersionUID = 1L;
    protected User you;
	protected Set<User> others;
	
	public UserGroup()
	{
		others = new TreeSet<User>();
	}
	
	public synchronized void addSelf(User you)
	{
		this.you = you;
		this.group.sendUpdate(new NewUserUpdate(getKey(), this));
		you.addObserver(this);
        this.setChanged();
        this.notifyObservers();
	}
	
	protected synchronized void addAnother(User other/*, UserPermissions permissions*/)
	{
		others.add(other);
		other.addObserver(this);
        this.setChanged();
        this.notifyObservers();
	}
    
    public synchronized User getSelf()
    {
        return you;
    }
    
    public synchronized Set<User> getUsers()
    {
        TreeSet<User> users = new TreeSet<User>();
        users.add(you);
        for(User u : others)
        {
            users.add(u);
        }
        return users;
    }
	
	public synchronized int getSize()
	{
	    return others.size() + 1;
	}
	
	public void makeNotification()
	{
        this.setChanged();
        this.notifyObservers();
	}

    @Override
    public void update(Observable arg0, Object arg1)
    {
        this.setChanged();
        this.notifyObservers();
    }
}
